package com.zenithmind.library.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 图书作者关联实体
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(name = "图书作者关联实体", description = "图书与作者的关联关系")
@TableName("lib_book_author")
public class BookAuthor extends BaseEntity {

    @NotBlank(message = "图书ID不能为空")
    @Schema(description = "图书ID")
    private String bookId;

    @NotBlank(message = "作者ID不能为空")
    @Schema(description = "作者ID")
    private String authorId;

    @Size(max = 50, message = "作者角色长度不能超过50个字符")
    @Schema(description = "作者角色")
    private String authorRole;

    @Schema(description = "排序号")
    private Integer sortOrder;

    /**
     * 判断是否为主要作者
     */
    public boolean isPrimaryAuthor() {
        return sortOrder != null && sortOrder == 1;
    }

    /**
     * 判断是否有作者角色
     */
    public boolean hasAuthorRole() {
        return authorRole != null && !authorRole.trim().isEmpty();
    }

    /**
     * 获取作者角色名称（如果为空则返回默认值）
     */
    public String getAuthorRoleName() {
        if (authorRole != null && !authorRole.trim().isEmpty()) {
            return authorRole;
        }
        return isPrimaryAuthor() ? "主要作者" : "作者";
    }

    /**
     * 判断是否为合作作者
     */
    public boolean isCoAuthor() {
        return !isPrimaryAuthor();
    }

    /**
     * 获取排序权重（用于排序）
     */
    public int getSortWeight() {
        return sortOrder != null ? sortOrder : Integer.MAX_VALUE;
    }
}
